
#include "LoseGameScreen.h"


using namespace std;
//using namespace cgGame;


CLoseGameScreen::CLoseGameScreen(
	HWND hwnd, 
	LPDIRECT3DDEVICE9 d3dDevice)
	:m_hWnd(hwnd), 
	m_pD3DDevice(d3dDevice),
	m_lButtonUp(true),
	m_lButtonDown(false),
	m_pBgSprite(NULL),
	m_pGameState(NULL),
	
	m_pRestartSprite(NULL),
	m_pRestartOverSprite(NULL),
	m_pRestartDownSprite(NULL),

	m_pAnimalNormalSprite(NULL),
	m_pAnimalEdibleSprite(NULL),
	m_pAnimalBigSprite(NULL),
	m_pAnimalFastSprite(NULL),
	
	m_pFontRender(NULL)
{

	m_countDownNum = 0.0f;
	m_countDownInterval = 0.4f;

	m_numKillAnimBig = 0;
	m_numKillAnimFast = 0;
	m_numKillAnimEat = 0;
	m_numKillAnimNormal = 0;
	m_textOutStr = "";

	m_timeRestartOffset = 1.0f;
	m_restart = false;

	GetCursorPos(&m_currMousrPos);
};


CLoseGameScreen::~CLoseGameScreen()
{
	SafeDelete(m_pFontRender);

	SafeDelete(m_pAnimalFastSprite);
	SafeDelete(m_pAnimalBigSprite);
	SafeDelete(m_pAnimalEdibleSprite);
	SafeDelete(m_pAnimalNormalSprite);

	SafeDelete(m_pRestartDownSprite);
	SafeDelete(m_pRestartOverSprite);
	SafeDelete(m_pRestartSprite);

	SafeDelete(m_pBgSprite);
}



bool CLoseGameScreen::InitScreenObjects()
{
	m_pGameState = CGameState::GetGameStateInstance();

	m_pBgSprite = new CD3DXSprite(m_pD3DDevice);
	m_pBgSprite->SetTextureScale("res\\menu\\losegamebg.png", g_screenWidth, g_screenHeight);
	m_pBgSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 2, g_screenHeight / 2));

	//menu
	m_pRestartSprite = new CD3DXSprite(m_pD3DDevice);
	m_pRestartSprite->SetTextureScale("res\\menu\\restart.png", 200, 100);
	m_pRestartSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 2 , g_screenHeight / 6 * 5 + 50));
	
	m_pRestartOverSprite = new CD3DXSprite(m_pD3DDevice);
	m_pRestartOverSprite->SetTextureScale("res\\menu\\restartover.png", 200, 100);
	m_pRestartOverSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 2 , g_screenHeight / 6 * 5+ 50));

	m_pRestartDownSprite = new CD3DXSprite(m_pD3DDevice);
	m_pRestartDownSprite->SetTextureScale("res\\menu\\restartdown.png", 200, 100);
	m_pRestartDownSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 2 , g_screenHeight / 6 * 5+ 50));

	//animal normal sprite
	m_pAnimalNormalSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalNormalSprite->SetTextureScale("res\\pic\\animal2.png", g_animalSize, g_animalSize);
	//animal edible sprite
	m_pAnimalEdibleSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalEdibleSprite->SetTextureScale("res\\pic\\animaledible2.png", g_animalSize, g_animalSize);
	//animal big sprite
	m_pAnimalBigSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalBigSprite->SetTextureScale("res\\pic\\animalbig2.png", g_animalSize, g_animalSize);
	//animal fase sprite
	m_pAnimalFastSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalFastSprite->SetTextureScale("res\\pic\\animalfast2.png", g_animalSize, g_animalSize);


	m_pFontRender = new CFontRenderer(m_pD3DDevice, 24);

	////debug
	//m_pDebug = new CDebug();

	return true;
}



void CLoseGameScreen::RenderScore(float timeDelta)
{
	m_countDownNum -= timeDelta;

	m_pAnimalNormalSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 6, g_screenHeight / 6));
	m_pAnimalNormalSprite->Render(timeDelta);
	if (m_countDownNum <= 0 &&
		m_numKillAnimNormal < m_pGameState->GetKillAnimNum(Character_Type_Animal_Normal))
	{
		m_countDownNum = m_countDownInterval;
		m_numKillAnimNormal ++;
		m_pGameState->SetMenuClickSound(true);
	}

	m_pAnimalFastSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 6 , g_screenHeight / 6 * 2));
	m_pAnimalFastSprite->Render(timeDelta);
	if (m_countDownNum <= 0 &&
		m_numKillAnimNormal == m_pGameState->GetKillAnimNum(Character_Type_Animal_Normal) &&
		m_numKillAnimFast < m_pGameState->GetKillAnimNum(Character_Type_Animal_Fast))
	{
		m_countDownNum = m_countDownInterval;
		m_numKillAnimFast ++;
		m_pGameState->SetMenuClickSound(true);
	}

	m_pAnimalEdibleSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 6 , g_screenHeight / 6 * 3));
	m_pAnimalEdibleSprite->Render(timeDelta);
	if (m_countDownNum <= 0 &&
		m_numKillAnimFast == m_pGameState->GetKillAnimNum(Character_Type_Animal_Fast) &&
		m_numKillAnimEat < m_pGameState->GetKillAnimNum(Character_Type_Animal_Eat))
	{
		m_countDownNum = m_countDownInterval;
		m_numKillAnimEat ++;
		m_pGameState->SetMenuClickSound(true);
	}

	m_pAnimalBigSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 6 , g_screenHeight / 6 * 4));
	m_pAnimalBigSprite->Render(timeDelta);
	if (m_countDownNum <= 0 &&
		m_numKillAnimEat == m_pGameState->GetKillAnimNum(Character_Type_Animal_Eat) &&
		m_numKillAnimBig < m_pGameState->GetKillAnimNum(Character_Type_Animal_Big))
	{
		m_countDownNum = m_countDownInterval;
		m_numKillAnimBig ++;
		m_pGameState->SetMenuClickSound(true);
	}

	//m_textOutStr = FloatToString(g_scoreAnimalNormal) + " X " + FloatToString(m_numKillAnimNormal) +
	//	"\n\n\n\n" + FloatToString(g_scoreAnimalFast) + " X " + FloatToString(m_numKillAnimFast) +
	//	"\n\n\n\n" + FloatToString(g_scoreAnimalEat) + " X " + FloatToString(m_numKillAnimEat) +
	//	"\n\n\n\n" + FloatToString(g_scoreAnimalBig) + " X " + FloatToString(m_numKillAnimBig) ;
	m_textOutStr =   tostr(g_scoreAnimalNormal) + " X " + tostr(m_numKillAnimNormal) +
		"\n\n\n\n" + tostr(g_scoreAnimalFast) + " X "   + tostr(m_numKillAnimFast) +
		"\n\n\n\n" + tostr(g_scoreAnimalEat) + " X "    + tostr(m_numKillAnimEat) +
		"\n\n\n\n" + tostr(g_scoreAnimalBig) + " X "    + tostr(m_numKillAnimBig) +
		"\n\nTotal Score: " + tostr(m_pGameState->GetGameScore());
}



void CLoseGameScreen::RenderObjects(float timeDelta)
{

	if (Game_CurrState_Lose == m_pGameState->GetGamecurrState())
	{
		m_pBgSprite->Render(timeDelta);

		RenderScore(timeDelta);

		m_pFontRender->Render(D3DXVECTOR3(g_screenWidth / 2, g_screenHeight / 2, 0),
			//"Game Level : " + cgMath::FloatToString((float)m_pGameState->GetGameLevel()),
			m_textOutStr,
			true, 
			D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f),
			true,
			D3DXVECTOR2(g_screenWidth / 6 * 2, g_screenHeight / 6));

		GetCursorPos(&m_currMousrPos);
		ScreenToClient(m_hWnd, &m_currMousrPos);

		
		if (m_numKillAnimBig == m_pGameState->GetKillAnimNum(Character_Type_Animal_Big) &&
			m_numKillAnimFast == m_pGameState->GetKillAnimNum(Character_Type_Animal_Fast) &&
			m_numKillAnimNormal == m_pGameState->GetKillAnimNum(Character_Type_Animal_Normal) &&
			m_numKillAnimEat == m_pGameState->GetKillAnimNum(Character_Type_Animal_Eat))
		{
			if (m_currMousrPos.x < m_pRestartSprite->GetLeftTopPos().x ||
				m_currMousrPos.y < m_pRestartSprite->GetLeftTopPos().y ||
				m_currMousrPos.x > m_pRestartSprite->GetRightDownPos().x || 
				m_currMousrPos.y > m_pRestartSprite->GetRightDownPos().y)
			{
				m_pRestartSprite->Render(timeDelta);
			}
			else
			{
				m_pRestartOverSprite->Render(timeDelta);
				if (KEYDOWN(VK_LBUTTON) && m_lButtonUp )
				{
					m_pRestartDownSprite->Render(timeDelta);
					m_pGameState->SetMenuClickSound(true);
					m_lButtonUp = false;
					//m_pRestartSprite->Render(timeDelta);
					//m_pGameState->SetStartNewGameSound(true);
					//m_pGameState->SetGameCurrState(Game_CurrState_Prepareing);
					//m_pGameState->InitGameState(0, 0);
				}
				if (KEYUP(VK_LBUTTON) && m_lButtonDown)
				{
					m_pRestartDownSprite->Render(timeDelta);
					m_lButtonDown = false;
					m_restart = true;

				}
			}
		}

		if (true == m_restart)
		{
			m_timeRestartOffset -= timeDelta;
			if (m_timeRestartOffset <= 0)
			{
				m_timeRestartOffset = 5.0f;
				m_restart = false;
				m_pRestartSprite->Render(timeDelta);
				m_pGameState->SetStartNewGameSound(true);
				m_pGameState->SetGameCurrState(Game_CurrState_Prepareing);
				m_pGameState->InitGameState(0, 0);
			}
		}

		if (KEYUP(VK_LBUTTON))
		{
			m_lButtonUp = true;
		}
		else
		{
			m_lButtonUp = false;
		}
		if (KEYDOWN(VK_LBUTTON))
		{
			m_lButtonDown = true;
		}
		else
		{
			m_lButtonDown = false;
		}
	}
	else
	{
		m_numKillAnimBig = 0;
		m_numKillAnimFast = 0;
		m_numKillAnimEat = 0;
		m_numKillAnimNormal = 0;
		m_textOutStr = "";
	}

}